//
//  HMVisitorView.swift
//  Weibo
//
//  Created by 米易雅克 on 16/4/9.
//  Copyright © 2016年 米易雅克. All rights reserved.
//

import UIKit
//MARK: 定义代理协议
protocol HMVisitorViewDelegate :NSObjectProtocol{

//定义代理方法
    func visitorViewDidClickRegisterButton()
    
    func visitorViewDidClickLoginButton()
}

class HMVisitorView: UIView {
    
    
//    定义代理属性
    weak var delegate: HMVisitorViewDelegate?
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        prepareUI()
        backgroundColor = UIColor(white: 237/255.0, alpha : 1)
    
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   
// 准备UI
    private func prepareUI() {
//  添加到父控件上
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
//  添加约束
//        禁用自动布局
       iconView.translatesAutoresizingMaskIntoConstraints = false
       coverView.translatesAutoresizingMaskIntoConstraints = false
       homeView.translatesAutoresizingMaskIntoConstraints = false
       messageLabel.translatesAutoresizingMaskIntoConstraints = false
       registerButton.translatesAutoresizingMaskIntoConstraints = false
       loginButton.translatesAutoresizingMaskIntoConstraints = false
//        NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
//      水平居中
        self.addConstraint( NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
//    遮盖
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
//        房子
        
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
//        消息文字
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 250))
//        注册按钮
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 30))
//        登陆按钮
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0))
        
        
        
        
    }
    func setVisitorViewInfo(ImageName : String,Message : String) -> () {
        
//        设置转轮图片
        iconView.image = UIImage.init(named: ImageName)
        messageLabel.text = Message
        homeView.hidden = true
        coverView.hidden = true
        
    }
    
//  转盘转动
    func startRation()  {
        let aim = CABasicAnimation(keyPath: "transform.rotation")
        aim.repeatCount = MAXFLOAT
        aim.toValue = M_PI * 2
        aim.duration = 20
        aim.removedOnCompletion = false
        iconView.layer.addAnimation(aim, forKey: nil)
    }
    
    
//MARK:  懒加载
//    转盘
    private lazy var iconView : UIImageView = UIImageView(image: UIImage.init(named: "visitordiscover_feed_image_smallicon"))
//  遮盖
    private lazy var coverView : UIImageView = UIImageView(image: UIImage.init(named: "visitordiscover_feed_mask_smallicon"))
 
//小房子
    private lazy var homeView : UIImageView = UIImageView(image: UIImage.init(named: "visitordiscover_feed_image_house"))
// 消息文字
    private lazy var messageLabel : UILabel = {
    
        let label = UILabel()
        
        label.text = "关注一些人!看看有神马惊喜!"
        label.font = UIFont.systemFontOfSize(18)
        label.numberOfLines = 0
        label.textAlignment = NSTextAlignment.Center
        
    
        return label
    }()

//    注册按钮
    private lazy var registerButton : UIButton = {
    
        let button = UIButton()
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage.init(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
//        
        button.addTarget(self, action: #selector(HMVisitorView.didClickRegistButton), forControlEvents: UIControlEvents.TouchUpInside)
//         button.addTarget(self, action: #selector(HMVisitorView.didClickRegistButton), forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()

//登陆按钮
    private lazy var loginButton : UIButton = {
        let button = UIButton()
        button.setTitle("登陆", forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage.init(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        
//        button.addTarget(self, action: #selector(HMVisitorView.didClickLoginButton), forControlEvents: UIControlEvents.TouchUpOutside)
         button.addTarget(self, action: #selector(HMVisitorView.didClickLoginButton), forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
//    MARK: 注册按钮点击事件
    @objc private func didClickRegistButton() {
        
        delegate?.visitorViewDidClickRegisterButton()
        
    }
     @objc private func didClickLoginButton() {
        
        delegate?.visitorViewDidClickLoginButton()
    }
    
}
